import { Router } from "express"
import { IcallbackFn, Ioracle } from "../../types"

export default (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {
  //抓取盘点/盘差报告
  router.post("/getReportList", (req, res) => {
    const { orgCode, deptCode, checkMonth,account,power } = req.body
    let sql = " select rownum seq,header_id,check_month "
    sql += " ,dept_code,chinese_name "
    sql += " ,decode(flow_phase,'close','簽核完成',flow_phase) flow_phase "
    sql += " from "
    sql += " (select * "
    sql += " from xx_general_asset_check_header a,user_info_n b "
    sql += " where 1 = 1 "
    sql += " and a.created_by = b.account "
    sql += " and a.check_type is not null "
    if(power !== "Admin") {
      sql += " and exists ( "
      sql += " select * from xx_general_asset_check_flow "
      sql += " where header_id = a.header_id "
      sql += " and action_klm = '"+ account +"' "
      sql += " ) "
    }
    sql += orgCode && " and dept_code like '" + orgCode + "%' "
    sql += deptCode && " and dept_code = '" + deptCode + "' "
    sql += checkMonth && " and check_month = '" + checkMonth + "' "
    sql += " order by a.check_month,a.dept_code) "
    callbackFn(res, oracle.connChaos,{sql})
  })

  //抓取细项
  router.post("/getReportCnt", (req, res) => {
    const { headerId, dataType, dataAsset,dataSub } = req.body
    let sql = " select a.asset_no,a.asset_name "
    sql += " ,decode(a.asset_type,'A','已入籍','Exist','待驗收','Rent','租賃','Free','無償借用','Offer','客供',a.asset_type) asset_type "
    sql += " ,to_char(b.get_date,'yyyy/mm/dd') get_date,b.asset_cost,b.remain_money "
    sql += " ,a.lose_reason,a.lose_action "
    sql += " from xx_general_asset_check_line a,xx_general_asset_equip_list b"
    sql += " where a.header_id = '" + headerId + "' "
    sql += " and a.asset_no = b.asset_no(+) "
    sql += dataType === "check_cnt" ? " and a.check_result is not null " : ""
    sql += dataType === "leave_cnt" ? " and a.lose_reason is not null " : ""
    sql += dataAsset !== "Total" ? " and a.asset_type = '" + dataAsset + "' " : ""
    sql += dataSub !== "undefined" ? " and a.asset_no like '"+ dataSub +"%' " : ""
    sql += " order by asset_no "
    callbackFn( res, oracle.connChaos,{sql})
  })

  return router
}
